package com.cos.hospital.ui.patient.compare;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cos.hospital.R;
import com.cos.hospital.model.bean.AcupointBean;
import com.cos.hospital.model.bean.RecordWebBean;
import com.cos.hospital.tool.UDate;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.tool.glide.UGlide;
import com.cos.hospital.ui.patient.compare.bean.ABBean;
import com.cos.hospital.ui.patient.compare.bean.SingleBean;
import com.cos.hospital.ui.record.RecordCode;

import java.util.List;

/**
 * @Description: <p>
 * @Author: Kosmos
 * @Date: 2018年09月18日 20:59
 * @Email: KosmoSakura@foxmail.com
 */
public class ABAdapter extends BaseMultiItemQuickAdapter<ABBean, BaseViewHolder> {
    private String emptyTxt = "无对应病历";
    private Context context;

    public ABAdapter(@Nullable List<ABBean> data, Context context) {
        super(data);
        this.context = context;
        addItemType(RecordCode.Type.ChiefDescribe, R.layout.item_ab_text);//主诉
        addItemType(RecordCode.Type.CurrentMedicalHistory, R.layout.item_ab_text);//现病史
        addItemType(RecordCode.Type.PastHistory, R.layout.item_ab_text);//既往史
        addItemType(RecordCode.Type.AllergyHistory, R.layout.item_ab_text);//过敏史
        addItemType(RecordCode.Type.MenstruationHistory, R.layout.item_ab_mest);//经产史
        addItemType(RecordCode.Type.TonguePulse, R.layout.item_ab_image);//舌脉象
        addItemType(RecordCode.Type.Diagnosis, R.layout.item_ab_text);//辩证诊断
        addItemType(RecordCode.Type.Prescription, R.layout.item_ab_image);//治疗处方
        addItemType(RecordCode.Type.AcupunctureRecord, R.layout.item_ab_acu);//针灸记录
    }

    @Override
    protected void convert(BaseViewHolder helper, ABBean item) {
        switch (helper.getItemViewType()) {
            case RecordCode.Type.ChiefDescribe://主诉:
            case RecordCode.Type.CurrentMedicalHistory://现病史:
            case RecordCode.Type.Diagnosis://辩证诊断:
            case RecordCode.Type.PastHistory://既往史:
            case RecordCode.Type.AllergyHistory://过敏史:
                helper.setText(R.id.ab_tag, UTxt.isNull(item.getTagName()));
                SingleBean abean = item.getaRod();
                SingleBean bbean = item.getbRod();
                if (abean == null) {
                    setHint(helper.getView(R.id.ab_t_a));
                } else {
                    setText(helper.getView(R.id.ab_t_a), UTxt.isNull(abean.getMainText()));
                }
                if (bbean == null) {
                    setHint(helper.getView(R.id.ab_t_b));
                } else {
                    setText(helper.getView(R.id.ab_t_b), UTxt.isNull(bbean.getMainText()));
                }
                break;
            case RecordCode.Type.TonguePulse://舌脉象:
            case RecordCode.Type.Prescription://治疗处方:
                helper.setText(R.id.ab_tag, UTxt.isNull(item.getTagName()));
                inclouldTnggueData(helper, item.getaRod(), item.getbRod());
                break;
            case RecordCode.Type.MenstruationHistory://经产史:
                helper.setText(R.id.ab_tag, UTxt.isNull(item.getTagName()));
                inclouldMensData(helper, item.getaRod(), item.getbRod());
                break;
            case RecordCode.Type.AcupunctureRecord://针灸记录:
                helper.setText(R.id.ab_tag, UTxt.isNull(item.getTagName()));
                inclouldAcuData(helper, item.getaRod(), item.getbRod());
                break;
        }
    }

    /**
     * 针灸记录
     */
    private void inclouldAcuData(BaseViewHolder helper, SingleBean aBean, SingleBean bBean) {
        GridView aRc = helper.getView(R.id.ab_rc_a);
        TextView tA = helper.getView(R.id.ab_t_a);
        TextView oA = helper.getView(R.id.other_treata);
        TextView oB = helper.getView(R.id.other_treatb);
        if (aBean != null) {
            List<AcupointBean> aList = aBean.getAcuList();
            if (!UTxt.isEmpty(aList)) {
                aRc.setVisibility(View.VISIBLE);
                tA.setVisibility(View.GONE);
                aRc.setAdapter(new AcuAdapter(aList, context));
            } else {
                aRc.setVisibility(View.GONE);
                setHint(tA);
            }
            oA.setText(TextUtils.isEmpty(aBean.getFldTreatmentDesc()) ? "其他治疗:无" : ("其他治疗:" + aBean.getFldTreatmentDesc()));
            oA.setVisibility(View.VISIBLE);
        } else {
            aRc.setVisibility(View.GONE);
            setHint(tA);
            oA.setVisibility(View.GONE);
        }

        GridView bRc = helper.getView(R.id.ab_rc_b);
        TextView tB = helper.getView(R.id.ab_t_b);
        if (bBean != null) {
            List<AcupointBean> bList = bBean.getAcuList();
            if (!UTxt.isEmpty(bList)) {
                bRc.setVisibility(View.VISIBLE);
                tB.setVisibility(View.GONE);
                bRc.setAdapter(new AcuAdapter(bList, context));
            } else {
                bRc.setVisibility(View.GONE);
                setHint(tB);
            }
            oB.setText(TextUtils.isEmpty(bBean.getFldTreatmentDesc()) ? "其他治疗:无" : ("其他治疗:" + bBean.getFldTreatmentDesc()));
            oB.setVisibility(View.VISIBLE);
        } else {
            bRc.setVisibility(View.GONE);
            setHint(tB);
            oB.setVisibility(View.GONE);
        }
    }


    /**
     * @apiNote 舌脉象、治疗处方
     */
    private void inclouldTnggueData(BaseViewHolder helper, SingleBean aBean, SingleBean bBean) {
        if (aBean != null) {
            if (UTxt.isEmpty(aBean.getMainText())) {
                helper.getView(R.id.ab_t_a).setVisibility(View.GONE);
            } else {
                setText(helper.getView(R.id.ab_t_a), aBean.getMainText());
            }
            List<String> aList = aBean.getImageList();
            int tongueSize = UTxt.getSize(aList);
            View i_line = helper.getView(R.id.ab_image_a_lay);
            ImageView i_1 = helper.getView(R.id.ab_image_a_1);
            ImageView i_2 = helper.getView(R.id.ab_image_a_2);
            ImageView i_3 = helper.getView(R.id.ab_image_a_3);
            helper.addOnClickListener(R.id.ab_image_a_1);
            helper.addOnClickListener(R.id.ab_image_a_2);
            helper.addOnClickListener(R.id.ab_image_a_3);
            if (tongueSize < 1) {
                i_line.setVisibility(View.GONE);
                i_1.setVisibility(View.INVISIBLE);
                i_2.setVisibility(View.INVISIBLE);
                i_3.setVisibility(View.INVISIBLE);
            } else if (tongueSize == 1) {
                i_line.setVisibility(View.VISIBLE);
                i_1.setVisibility(View.VISIBLE);
                i_2.setVisibility(View.INVISIBLE);
                i_3.setVisibility(View.INVISIBLE);
                UGlide.loadRound(helper.itemView.getContext(), aList.get(0), i_1);
            } else if (tongueSize == 2) {
                i_line.setVisibility(View.VISIBLE);
                i_1.setVisibility(View.VISIBLE);
                i_2.setVisibility(View.VISIBLE);
                i_3.setVisibility(View.INVISIBLE);
                UGlide.loadRound(helper.itemView.getContext(), aList.get(0), i_1);
                UGlide.loadRound(helper.itemView.getContext(), aList.get(1), i_2);
            } else {
                i_line.setVisibility(View.VISIBLE);
                i_1.setVisibility(View.VISIBLE);
                i_2.setVisibility(View.VISIBLE);
                i_3.setVisibility(View.VISIBLE);
                UGlide.loadRound(helper.itemView.getContext(), aList.get(0), i_1);
                UGlide.loadRound(helper.itemView.getContext(), aList.get(1), i_2);
                UGlide.loadRound(helper.itemView.getContext(), aList.get(2), i_3);
            }
        } else {
            setHint(helper.getView(R.id.ab_t_a));
            helper.getView(R.id.ab_image_a_lay).setVisibility(View.GONE);
            helper.getView(R.id.ab_image_a_1).setVisibility(View.GONE);
            helper.getView(R.id.ab_image_a_2).setVisibility(View.GONE);
            helper.getView(R.id.ab_image_a_3).setVisibility(View.GONE);
        }
        if (bBean != null) {
            if (UTxt.isEmpty(bBean.getMainText())) {
                helper.getView(R.id.ab_t_b).setVisibility(View.GONE);
            } else {
                setText(helper.getView(R.id.ab_t_b), bBean.getMainText());
            }
            List<String> bList = bBean.getImageList();
            int tongueSize = UTxt.getSize(bList);
            View i_line = helper.getView(R.id.ab_image_b_lay);
            ImageView i_1 = helper.getView(R.id.ab_image_b_1);
            ImageView i_2 = helper.getView(R.id.ab_image_b_2);
            ImageView i_3 = helper.getView(R.id.ab_image_b_3);
            helper.addOnClickListener(R.id.ab_image_b_1);
            helper.addOnClickListener(R.id.ab_image_b_2);
            helper.addOnClickListener(R.id.ab_image_b_3);
            if (tongueSize < 1) {
                i_line.setVisibility(View.GONE);
                i_1.setVisibility(View.INVISIBLE);
                i_2.setVisibility(View.INVISIBLE);
                i_3.setVisibility(View.INVISIBLE);
            } else if (tongueSize == 1) {
                i_line.setVisibility(View.VISIBLE);
                i_1.setVisibility(View.VISIBLE);
                i_2.setVisibility(View.INVISIBLE);
                i_3.setVisibility(View.INVISIBLE);
                UGlide.loadRound(helper.itemView.getContext(), bList.get(0), i_1);
            } else if (tongueSize == 2) {
                i_line.setVisibility(View.VISIBLE);
                i_1.setVisibility(View.VISIBLE);
                i_2.setVisibility(View.VISIBLE);
                i_3.setVisibility(View.INVISIBLE);
                UGlide.loadRound(helper.itemView.getContext(), bList.get(0), i_1);
                UGlide.loadRound(helper.itemView.getContext(), bList.get(1), i_2);
            } else {
                i_line.setVisibility(View.VISIBLE);
                i_1.setVisibility(View.VISIBLE);
                i_2.setVisibility(View.VISIBLE);
                i_3.setVisibility(View.VISIBLE);
                UGlide.loadRound(helper.itemView.getContext(), bList.get(0), i_1);
                UGlide.loadRound(helper.itemView.getContext(), bList.get(1), i_2);
                UGlide.loadRound(helper.itemView.getContext(), bList.get(2), i_3);
            }
        } else {
            setHint(helper.getView(R.id.ab_t_b));
            helper.getView(R.id.ab_image_b_lay).setVisibility(View.GONE);
            helper.getView(R.id.ab_image_b_1).setVisibility(View.GONE);
            helper.getView(R.id.ab_image_b_2).setVisibility(View.GONE);
            helper.getView(R.id.ab_image_b_3).setVisibility(View.GONE);
        }
    }


    /**
     * @apiNote 月经史
     */
    private void inclouldMensData(BaseViewHolder helper, SingleBean aBean, SingleBean bBean) {
        View rootA = helper.getView(R.id.lmp_esl);
        if (aBean != null) {
            if (UTxt.isEmpty(aBean.getMainText())) {
                helper.getView(R.id.ab_t_a).setVisibility(View.GONE);
            } else {
                setText(helper.getView(R.id.ab_t_a), aBean.getMainText());
            }
            RecordWebBean.MhfBean mhfBean = aBean.getMhfBean();
            RecordWebBean.RhfBean rhfBean = aBean.getRhfBean();
            if (mhfBean == null && rhfBean == null) {
                rootA.setVisibility(View.GONE);
            } else {
                rootA.setVisibility(View.VISIBLE);
            }

            View mlp = helper.getView(R.id.formula_lmp_root);
            //月经史公式1
            if (mhfBean != null) {
                mlp.setVisibility(View.VISIBLE);
                helper.setText(R.id.formula_lmp_first, mhfBean.getFldMenarcheAge() + "岁");
                helper.setText(R.id.formula_lmp_long, mhfBean.getFldMinPeriod() + " - " + mhfBean.getFldMaxPeriod() + "天");
                helper.setText(R.id.formula_lmp_cycle, mhfBean.getFldMinCycle() + " - " + mhfBean.getFldMaxCycle() + "天");
                //公式类型(0以年龄,1日期)
                if (mhfBean.getFldType() == 1) {
                    helper.setText(R.id.formula_lmp_end, UDate.strToStrSafety(mhfBean.getFldLmp(), "yyyy-MM-dd"));
                } else {
                    helper.setText(R.id.formula_lmp_end, mhfBean.getFldMenarcheAge() + "岁");
                }
            } else {
                mlp.setVisibility(View.GONE);
            }
            //月经史公式2
            if (mhfBean != null) {
                mlp.setVisibility(View.VISIBLE);
                helper.setText(R.id.formula_lmp_first, mhfBean.getFldMenarcheAge() + "岁");
                helper.setText(R.id.formula_lmp_long, mhfBean.getFldMinPeriod() + " - " + mhfBean.getFldMaxPeriod() + "天");
                helper.setText(R.id.formula_lmp_cycle, mhfBean.getFldMinCycle() + " - " + mhfBean.getFldMaxCycle() + "天");
                //公式类型(0以年龄,1日期)
                if (mhfBean.getFldType() == 1) {
                    helper.setText(R.id.formula_lmp_end, UDate.strToStrSafety(mhfBean.getFldLmp(), "yyyy-MM-dd"));
                } else {
                    helper.setText(R.id.formula_lmp_end, mhfBean.getFldMenarcheAge() + "岁");
                }
            } else {
                mlp.setVisibility(View.GONE);
            }

            View gp = helper.getView(R.id.formula_gp_root);
            View br = helper.getView(R.id.formula_br_root);
            //生育史公式
            if (rhfBean != null) {
                //生育史
                if (rhfBean.getFldType() == 1) {
                    br.setVisibility(View.VISIBLE);//生育史
                    gp.setVisibility(View.GONE);//孕产史
                    helper.setText(R.id.formula_br_full, rhfBean.getFldTermDelivery() + "次");
                    helper.setText(R.id.formula_br_early, rhfBean.getFldPrematureDelivery() + "次");
                    helper.setText(R.id.formula_br_fail, rhfBean.getFldAbortion() + "次");
                    helper.setText(R.id.formula_br_alive, rhfBean.getFldChildrenNum() + "次");
                } else {
                    gp.setVisibility(View.VISIBLE);//孕产史
                    br.setVisibility(View.GONE);//生育史
                    helper.setText(R.id.formula_gp_g, rhfBean.getFldPregnancy() + "次");
                    helper.setText(R.id.formula_gp_plost, rhfBean.getFldParturition() + "次");
                    helper.setText(R.id.formula_gp_mark, UTxt.isNull(rhfBean.getFldDesc()));
                }
            } else {
                gp.setVisibility(View.GONE);//孕产史
                br.setVisibility(View.GONE);//生育史
            }
        } else {
            setHint(helper.getView(R.id.ab_t_a));
            rootA.setVisibility(View.GONE);
        }
        View rootB = helper.getView(R.id.lmp_esl_b);
        if (bBean != null) {
            if (UTxt.isEmpty(bBean.getMainText())) {
                helper.getView(R.id.ab_t_b).setVisibility(View.GONE);
            } else {
                setText(helper.getView(R.id.ab_t_b), bBean.getMainText());
            }
            RecordWebBean.MhfBean mhfBean = bBean.getMhfBean();
            RecordWebBean.RhfBean rhfBean = bBean.getRhfBean();
            if (mhfBean == null && rhfBean == null) {
                rootB.setVisibility(View.GONE);
            } else {
                rootB.setVisibility(View.VISIBLE);
            }

            View mlp = helper.getView(R.id.formula_lmp_root_b);
            //月经史公式1
            if (mhfBean != null) {
                mlp.setVisibility(View.VISIBLE);
                helper.setText(R.id.formula_lmp_first_b, mhfBean.getFldMenarcheAge() + "岁");
                helper.setText(R.id.formula_lmp_long_b, mhfBean.getFldMinPeriod() + " - " + mhfBean.getFldMaxPeriod() + "天");
                helper.setText(R.id.formula_lmp_cycle_b, mhfBean.getFldMinCycle() + " - " + mhfBean.getFldMaxCycle() + "天");
                //公式类型(0以年龄,1日期)
                if (mhfBean.getFldType() == 1) {
                    helper.setText(R.id.formula_lmp_end_b, UDate.strToStrSafety(mhfBean.getFldLmp(), "yyyy-MM-dd"));
                } else {
                    helper.setText(R.id.formula_lmp_end_b, mhfBean.getFldMenarcheAge() + "岁");
                }
            } else {
                mlp.setVisibility(View.GONE);
            }
            //月经史公式2
            if (mhfBean != null) {
                mlp.setVisibility(View.VISIBLE);
                helper.setText(R.id.formula_lmp_first_b, mhfBean.getFldMenarcheAge() + "岁");
                helper.setText(R.id.formula_lmp_long_b, mhfBean.getFldMinPeriod() + " - " + mhfBean.getFldMaxPeriod() + "天");
                helper.setText(R.id.formula_lmp_cycle_b, mhfBean.getFldMinCycle() + " - " + mhfBean.getFldMaxCycle() + "天");
                //公式类型(0以年龄,1日期)
                if (mhfBean.getFldType() == 1) {
                    helper.setText(R.id.formula_lmp_end_b, UDate.strToStrSafety(mhfBean.getFldLmp(), "yyyy-MM-dd"));
                } else {
                    helper.setText(R.id.formula_lmp_end_b, mhfBean.getFldMenarcheAge() + "岁");
                }
            } else {
                mlp.setVisibility(View.GONE);
            }

            View gp = helper.getView(R.id.formula_gp_root_b);
            View br = helper.getView(R.id.formula_br_root_b);
            //生育史公式
            if (rhfBean != null) {
                //生育史
                if (rhfBean.getFldType() == 1) {
                    br.setVisibility(View.VISIBLE);//生育史
                    gp.setVisibility(View.GONE);//孕产史
                    helper.setText(R.id.formula_br_full_b, rhfBean.getFldTermDelivery() + "次");
                    helper.setText(R.id.formula_br_early_b, rhfBean.getFldPrematureDelivery() + "次");
                    helper.setText(R.id.formula_br_fail_b, rhfBean.getFldAbortion() + "次");
                    helper.setText(R.id.formula_br_alive_b, rhfBean.getFldChildrenNum() + "次");
                } else {
                    gp.setVisibility(View.VISIBLE);//孕产史
                    br.setVisibility(View.GONE);//生育史
                    helper.setText(R.id.formula_gp_g_b, rhfBean.getFldPregnancy() + "次");
                    helper.setText(R.id.formula_gp_plost_b, rhfBean.getFldParturition() + "次");
                    helper.setText(R.id.formula_gp_mark_b, UTxt.isNull(rhfBean.getFldDesc()));
                }
            } else {
                gp.setVisibility(View.GONE);//孕产史
                br.setVisibility(View.GONE);//生育史
            }
        } else {
            setHint(helper.getView(R.id.ab_t_b));
            rootB.setVisibility(View.GONE);
        }
    }

    private void setText(TextView textView, String text) {
        textView.setVisibility(View.VISIBLE);
        textView.setText(text);
        textView.setTextColor(Color.parseColor("#494949"));
    }

    private void setHint(TextView textView) {
        textView.setVisibility(View.VISIBLE);
        textView.setText(emptyTxt);
        textView.setTextColor(Color.parseColor("#9a9a9a"));
    }
}
